﻿#define _CRT_SECURE_NO_WARNINGS
#include"snake.h"

//设置光标的位置
void SetPos(slDataType x, slDataType y)
{
	COORD pos = { x, y };
	HANDLE hOutput = NULL;
	//获取标准输出的句柄(⽤来标识不同设备的数值)
	hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	//设置标准输出上光标的位置为pos
	SetConsoleCursorPosition(hOutput, pos);
}

//打印欢迎界面
void Welcome()
{
	SetPos(40, 13);
	printf("欢迎来到贪吃蛇小游戏…");
	SetPos(42, 16);
	system("pause");
	system("cls");
	SetPos(24, 13);
	printf("用↑ . ↓ . ← . →分别控制蛇的移动方向，F3为加速，F4为减速\n");
	SetPos(42, 16);
	system("pause");
	system("cls");

}

//创建地图
CreateMap()
{
	//上
	SetPos(0,0);
	for (int i = 0; i < 29;i++)
	{
		wprintf(L"%lc", WALL);
	}
	//下
	SetPos(0, 27);
	for (int i = 0; i < 29; i++)
	{
		wprintf(L"%lc", WALL);
	}
	//左
	for (int j = 1; j <= 26; j++)
	{
		SetPos(0, j);
		wprintf(L"%lc", WALL);
	}
	//右
	for (int j = 1; j <= 26; j++)
	{
		SetPos(56, j);
		wprintf(L"%lc", WALL);
	}
}

//打印蛇身
void PrintBody(SnakeList* ps)
{
	SnakeList* pcur = ps;
	while (pcur)
	{
		SetPos(pcur->x, pcur->y);
		wprintf(L"%lc", BODY);
		pcur = pcur->next;
	}
}

//初始化蛇身
void InitSnake(Snake* ps)
{
	//创建蛇的身体节点
	SnakeList* pcur = NULL;
	for (int i = 0; i < 5; i++)
	{
		//动态申请空间
		pcur = (SnakeList*)malloc(sizeof(SnakeList));
		if (pcur == NULL)
		{
			perror("Init:malloc");
			exit(1);
		}
		//设置初值
		pcur->next = NULL;
		pcur->x = 24 + i * 2;
		pcur->y = 5;

		//头插法连接蛇的身体节点
		if (ps->psnake == NULL)//空表直接赋值
		{
			ps->psnake = pcur;
		}
		else//非空链表直接头插
		{
			pcur->next = ps->psnake;
			ps->psnake = pcur;
		}
	}
	//打印蛇身
	PrintBody(ps->psnake);
	//初始化贪吃蛇游戏的数据
	ps->dir = RIGHT;
	ps->score = 0;
	ps->SingleFoodScore = 10;
	ps->status = OK;
	ps->SleepTime = 200;
}

//打印食物坐标
void PrintFood(SnakeList* ps)
{
	SetPos(ps->x,ps->y);
	wprintf(L"%lc", FOOD);
}

//创建食物
void CreateFood(Snake* ps)
{
	//使用随机种子生成随机数建立坐标
	slDataType x;
	slDataType y;
	//保证坐标位置的有效性
	again:
	do
	{
		x = rand() % 53 + 2;
		y = rand() % 26 + 1;
	} while (x%2 != 0);

	//确保食物坐标不与蛇身节点重合
	SnakeList* cur = ps->psnake;//获取指向蛇头的指针
	while (cur)
	{
		if (cur->x == x && cur->y == y)
		{
			goto again;
		}
		cur = cur->next;
	}
	//申请食物节点
	SnakeList* pfood = (SnakeList*)malloc(sizeof(SnakeList));
	if (pfood == NULL)
	{
		perror("Create:malloc");
		exit(1);
	}
	//赋值坐标值
	pfood->next = NULL;
	pfood->x = x;
	pfood->y = y;
	//打印坐标
	PrintFood(pfood);
	//维护食物节点
	ps->pFood = pfood;
}

//游戏开始
void GameStart(Snake* ps)
{
	//打印欢迎界面
	Welcome();
	//创建地图
	CreateMap();
	//初始化蛇身
	InitSnake(ps);
	//创建食物
	CreateFood(ps);
}

//打印帮助信息
void PrintHelpInfo(Snake* ps)
{
	SetPos(64, 10);
	printf("得分：%d  ", ps->score);
	printf("每个食物得分：%d\n", ps->SingleFoodScore);
	SetPos(64, 12);
	printf("用↑ . ↓ . ← . →分别控制蛇的移动\n");
	SetPos(64, 13);
	printf("F3为加速，F4为减速\n");
	SetPos(64, 15);
	printf("ESC ：退出游戏.space：暂停游戏.");
}

//下一个位置有食物
int NextIsFood(SnakeList* psn,Snake* ps)
{
	return (psn->x == ps->pFood->x) && (psn->y == ps->pFood->y);
}
//吃食物
void EatFood(SnakeList* psn, Snake* ps)
{
	//头插法
	psn->next = ps->psnake;
	ps->psnake = psn;
	//打印蛇身
	PrintBody(ps->psnake);
	//加分
	ps->score += ps->SingleFoodScore;
	//释放食物节点
	free(ps->pFood);
	//创建新的食物节点
	CreateFood(ps);
}

//下一个节点没有食物
void NoFood(SnakeList* psn, Snake* ps)
{
	//头插法
	psn->next = ps->psnake;
	ps->psnake = psn;
	//打印蛇身,头插一个节点，最后一个节点覆盖为空格
	SnakeList* pcur = ps->psnake;
	while (pcur->next->next)
	{
		SetPos(pcur->x, pcur->y);
		wprintf(L"%lc", BODY);
		pcur = pcur->next;
	}
	//覆盖尾打印节点
	SetPos(pcur->next->x, pcur->next->y);
	printf("  ");
	//释放尾节点
	free(pcur->next);
	pcur->next = NULL;
}

//撞墙
int KILLBYWALL(Snake* ps)
{
	if ((ps->psnake->x == 0)
		|| (ps->psnake->x == 56)
		|| (ps->psnake->y == 0)
		|| (ps->psnake->y == 27))
	{
		ps->status = KILL_BY_WALL;
		return 1;
	}
	return 0;
}
//撞自己
int KILLBYSELF(Snake* ps)
{
	SnakeList* pcur = ps->psnake->next;
	while (pcur)
	{
		if ((pcur->x == ps->psnake->x)
			&& (ps->psnake->y == pcur->y))
		{
			ps->status = KILL_BY_SELF;
			return 1;
		}
		pcur = pcur->next;
	}
	return 0;
}


//蛇身移动
void SnakeMove(Snake* ps)
{
	//蛇向前移动的原理是创建一个新节点，并头插后删除尾节点，空格覆盖之前的尾节点打印
	SnakeList* nextnode = (SnakeList*)malloc(sizeof(SnakeList));
	if (nextnode == NULL)
	{
		perror("SnakeMove:malloc");
		exit(1);
	}
	//确定下一个节点的坐标，下一个节点的坐标根据蛇头的坐标和方向确定
	switch(ps->dir)
	{
	case UP:
	{
		nextnode->x = ps->psnake->x;
		nextnode->y = ps->psnake->y - 1;
	}
	break;
	case DOWN:
	{
		nextnode->x = ps->psnake->x;
		nextnode->y = ps->psnake->y + 1;
	}
	break;
	case LEFT:
	{
		nextnode->x = ps->psnake->x - 2;
		nextnode->y = ps->psnake->y;
	}
	break;
	case RIGHT:
	{
		nextnode->x = ps->psnake->x + 2;
		nextnode->y = ps->psnake->y;
	}
	break;
	}

	//下一个位置是食物的情况
	if (NextIsFood(nextnode, ps))
	{
		EatFood(nextnode,ps);
	}
	else//下一个位置没有食物的情况
	{
		NoFood(nextnode,ps);
	}

	//撞墙
	KILLBYWALL(ps);
	//撞自己
	KILLBYSELF(ps);
}

//游戏运行(游戏逻辑)
void GameRun(Snake* ps)
{
	//打印帮助信息
	PrintHelpInfo(ps);
	//设置虚拟按键
	do
	{
		if (KEY_PRESS(VK_UP) && ps->dir != DOWN)
		{
			ps->dir = UP;
		}
		else if (KEY_PRESS(VK_DOWN) && ps->dir != UP)
		{
			ps->dir = DOWN;
		}
		else if (KEY_PRESS(VK_LEFT) && ps->dir != RIGHT)
		{
			ps->dir = LEFT;
		}
		else if (KEY_PRESS(VK_RIGHT) && ps->dir != LEFT)
		{
			ps->dir = RIGHT;
		}
		else if (KEY_PRESS(VK_SPACE))
		{
			SetPos(0, 28);
			system("pause");
		}
		else if (KEY_PRESS(VK_ESCAPE))
		{
			ps->status = END_NORMAL;
			break;
		}
		else if (KEY_PRESS(VK_F3))
		{
			//休眠最短50ms，单个食物最高20分
			if (ps->SleepTime >= 80)
			{
				ps->SleepTime -= 30;
				ps->SingleFoodScore += 2;
			}
		}
		else if (KEY_PRESS(VK_F4))
		{
			//休眠最高320ms，单个食物最低2分
			if (ps->SleepTime < 320)
			{
				ps->SleepTime += 30;
				ps->SingleFoodScore -= 2;
			}
		}
		
		//休眠
		Sleep(ps->SleepTime);
		//蛇身移动
		SnakeMove(ps);
	} while (ps->status == OK);
}

//游戏结束(资源释放)
void GameEnd(Snake* ps)
{
	SetPos(24, 12);
	switch(ps->status)
	{
	case END_NORMAL:
		printf("您主动退出游戏\n");
		break;
	case KILL_BY_SELF:
		printf("您撞上自己了 ,游戏结束!\n");
		break;
	case KILL_BY_WALL:
		printf("您撞墙了,游戏结束!\n");
		break;
	}

	//释放蛇的节点
	SnakeList* pcur = ps->psnake;
	while (pcur)
	{
		SnakeList* del = pcur;
		pcur = pcur->next;
		free(del);
	}
}